package com.lemon.smartsafetynew.ui.adapter.training;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.training.OnlineVideoVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2018/3/15.
 */

public class OnlineTrainingVideoAdapter extends LemonRcvAdapter<OnlineVideoVo> {

    private RecyclerTClickListener<OnlineVideoVo> mListener;

    public OnlineTrainingVideoAdapter(@Nullable List<OnlineVideoVo> data,
                                      RecyclerTClickListener<OnlineVideoVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new VideoItem();
    }

    public class VideoItem implements AdapterItem<OnlineVideoVo> {

        private OnlineVideoVo videoVo;
        private int pos;

        @Bind(R.id.video_titleImageView)
        ImageView videoTitleImageView;
        @Bind(R.id.video_titleName)
        TextView videoTitleName;
        @Bind(R.id.video_item_layout)
        FrameLayout videoItemLayout;

        @Override
        public int getLayoutResId() {
            return R.layout.item_online_training_video_recycler_item;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            videoItemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(videoVo, pos);
                }
            });
        }

        @Override
        public void handleData(OnlineVideoVo onlineVideoVo, int position) {

            pos = position;
            videoVo = onlineVideoVo;

            videoTitleName.setText(CommonUtil.avoidNullString(onlineVideoVo.getVideoName()));

            Glide.with(videoTitleImageView.getContext())
                    .load(onlineVideoVo.getVideoImage())
                    .placeholder(R.mipmap.icon_video_default_view)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(videoTitleImageView);
        }
    }
}
